/**
 *
 * Created : Feb 23, 2012
 *
 * @author pquiring
 */

import java.io.*;
import java.util.*;

import javaforce.*;

import jfparted.*;

public class GetUserDetails extends IPanel {

  /**
   * Creates new form GetUserDetails
   */
  public GetUserDetails() {
    initComponents();
    if (Data.fullName != null) {
      fullName.setText(Data.fullName);
    }
    if (Data.loginName != null) {
      loginName.setText(Data.loginName);
    }
    if (Data.localhost == null)
      localhost.setText("computer" + Math.abs(new Random().nextInt(65535)));
    else
      localhost.setText(Data.localhost);
    if (Data.localdomain != null) {
      localdomain.setText(Data.localdomain);
    }
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        fullName = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        loginName = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        passwd1 = new javax.swing.JPasswordField();
        jLabel4 = new javax.swing.JLabel();
        passwd2 = new javax.swing.JPasswordField();
        jLabel5 = new javax.swing.JLabel();
        localhost = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        localdomain = new javax.swing.JTextField();

        jLabel1.setText("Full Name");

        jLabel2.setText("Login Name");

        jLabel3.setText("Password");

        jLabel4.setText("Confirm");

        jLabel5.setText("Computer Name");

        localhost.setText("localhost");

        jLabel6.setText("Domain");

        localdomain.setText("localdomain");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addGap(42, 42, 42)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(loginName, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(passwd1, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(passwd2)
                            .addComponent(fullName)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(localhost)
                            .addComponent(localdomain, javax.swing.GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(fullName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(loginName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(passwd1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(passwd2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(localhost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(localdomain, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(108, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField fullName;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField localdomain;
    private javax.swing.JTextField localhost;
    private javax.swing.JTextField loginName;
    private javax.swing.JPasswordField passwd1;
    private javax.swing.JPasswordField passwd2;
    // End of variables declaration//GEN-END:variables

  private String allChars(String str) {
    //allows all chars EXCEPT double-quotes
    return str.replaceAll("\"", "");
  }

  private String loginChars(String str) {
    String out = "";
    for(int a=0;a<str.length();a++) {
      char ch = str.charAt(a);
      if ((ch >= 'a') || (ch <= 'z')) {out += ch; continue;}
      if ((ch >= 'A') || (ch <= 'Z')) {out += ch; continue;}
      if ((ch >= '0') || (ch <= '9')) {out += ch; continue;}
    }
    return out;
  }

  private String domainChars(String str) {
    String out = "";
    for(int a=0;a<str.length();a++) {
      char ch = str.charAt(a);
      if ((ch >= 'a') || (ch <= 'z')) {out += ch; continue;}
      if ((ch >= 'A') || (ch <= 'Z')) {out += ch; continue;}
      if ((ch >= '0') || (ch <= '9')) {out += ch; continue;}
      if (ch == '.') {out += ch; continue;}
      if (ch == '-') {out += ch; continue;}
    }
    return out;
  }

  private boolean reserved(String login) {
    try {
      FileInputStream fis = new FileInputStream("/etc/passwd");
      int len = fis.available();
      byte buf[] = new byte[len];
      fis.read(buf);
      fis.close();
      String lns[] = new String(buf).split("\n");
      for(int a=0;a<lns.length;a++) {
        String f[] = lns[a].split(":");
        if (login.equalsIgnoreCase(f[0])) return true;
      }
      return false;
    } catch (Exception e) {
      JFLog.log(e);
      return true;
    }
  }

  public boolean valid() {
    fullName.setText(allChars(fullName.getText()));
    loginName.setText(loginChars(loginName.getText()));
    localhost.setText(loginChars(localhost.getText()));
    localdomain.setText(domainChars(localdomain.getText()));
    if (fullName.getText().length() < 3) {JFAWT.showError("Error", "Full name too short"); return false;}
    if (loginName.getText().length() < 3) {JFAWT.showError("Error", "Login name too short"); return false;}
    if (reserved(loginName.getText())) {JFAWT.showError("Error", "That login name is reserved"); return false;}
    if (new String(passwd1.getPassword()).length() < 5) {JFAWT.showError("Error", "Password too short"); return false;}
    if (!(new String(passwd1.getPassword())).equals(new String(passwd2.getPassword()))) {JFAWT.showError("Error", "Passwords do not match"); return false;}
    if (localhost.getText().length() < 3) {JFAWT.showError("Error", "Computer Name too short"); return false;}
    if (localdomain.getText().length() < 3) {JFAWT.showError("Error", "Domain too short"); return false;}
    return true;
  }

  public IPanel next() {
    if (!valid()) return null;
    Data.fullName = fullName.getText();
    Data.loginName = loginName.getText();
    Data.passwd = new String(passwd1.getPassword());
    Data.localhost = localhost.getText();
    Data.localdomain = localdomain.getText();
    return new TimeZone();
  }
  public IPanel prev() { return new Welcome(); }
  public IPanel getThis() {return this;}
}
